package com.shijie.newvideo;

import android.graphics.Typeface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.core.content.ContextCompat;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.shijie.newvideo.databinding.ActivityMainBinding;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MainActivity extends BaseActivity {
    private ActivityMainBinding binding;
    FragmentManager fragmentManager;
    FragmentTransaction fragmentTransaction;
    private int currentPageIndex = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_main);
        // 使用 View Binding 绑定布局
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        fragmentManager = getSupportFragmentManager();

//        binding.tv_request.setText("Hello, View Binding!");
        binding.tvRequest.setText("Hello, View Binding!");
        binding.tvRequest.setTextColor(getColor(R.color.color_ffffff));

        binding.rlMarket.setOnClickListener(this::onViewClicked);
        binding.rlUser.setOnClickListener(this::onViewClicked);
        binding.rlIndustry.setOnClickListener(this::onViewClicked);

        binding.checkboxMarket.setChecked(true);
        binding.rlMarket.performClick();

        binding.tvRequest.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 调用GET请求方法获取用户信息
                Call<VodBean> call = RetrofitClient.getApiService().getKanshipinglist("list", null);
                // 异步执行请求
                call.enqueue(new Callback<VodBean>() {
                    @Override
                    public void onResponse(Call<VodBean> call, Response<VodBean> response) {
                        if (response.isSuccessful()) {
                            VodBean vodBean = response.body();
                            Log.e("Network User: ", vodBean.toString());
                        } else {
                            Log.e("Network Error: ", response.message());
                        }
                    }

                    @Override
                    public void onFailure(Call<VodBean> call, Throwable t) {
                        Log.e("Network Error: ", t.getMessage());
                    }
                });

            }
        });

    }

    HomeMarketFragment homeMarketFragment;
    UserCenterFragment userCenterFragment;
    IndustryFragment industryFragment;

    public void onViewClicked(View view) {
        fragmentTransaction = fragmentManager.beginTransaction();
        if (view == binding.rlMarket) {
            currentPageIndex = 0;
            initCheckBox();
            hideFragments(fragmentTransaction);
            binding.textMarket.setTextColor(getColor(R.color.color_000000));
            binding.textMarket.setTypeface(Typeface.create(binding.textMarket.getTypeface(), Typeface.BOLD));
            if (homeMarketFragment == null) {
                homeMarketFragment = HomeMarketFragment.newInstance();
                fragmentTransaction.add(R.id.content_fragment, homeMarketFragment);
            } else {
                fragmentTransaction.show(homeMarketFragment);
            }
            fragmentTransaction.commitAllowingStateLoss();

        } else if (view == binding.rlIndustry) {
            currentPageIndex = 1;
            initCheckBox();
            hideFragments(fragmentTransaction);
            binding.textIndustry.setTextColor(getColor(R.color.color_000000));
            binding.textIndustry.setTypeface(Typeface.create(binding.textIndustry.getTypeface(), Typeface.BOLD));
            if (industryFragment == null) {
                industryFragment = IndustryFragment.newInstance();
                fragmentTransaction.add(R.id.content_fragment, industryFragment);
            } else {
                fragmentTransaction.show(industryFragment);
            }
            fragmentTransaction.commitAllowingStateLoss();


        } else if (view == binding.rlUser) {
            currentPageIndex = 2;
            initCheckBox();
            hideFragments(fragmentTransaction);
            binding.textUser.setTextColor(getColor(R.color.color_000000));
            binding.textUser.setTypeface(Typeface.create(binding.textUser.getTypeface(), Typeface.BOLD));
            if (userCenterFragment == null) {
                userCenterFragment = UserCenterFragment.newInstance();
                fragmentTransaction.add(R.id.content_fragment, userCenterFragment);
            } else {
                fragmentTransaction.show(userCenterFragment);
            }
            fragmentTransaction.commitAllowingStateLoss();

        }
    }

    private void hideFragments(FragmentTransaction transaction) {
        if (homeMarketFragment != null) {
            transaction.hide(homeMarketFragment);
        }
        if (industryFragment != null) {
            transaction.hide(industryFragment);
        }
        if (userCenterFragment != null) {
            transaction.hide(userCenterFragment);
        }

    }

    public void initCheckBox() {
        binding.checkboxIndustry.setChecked(false);
        binding.checkboxMarket.setChecked(false);
        binding.checkboxUser.setChecked(false);

        binding.textIndustry.setTextColor(mContext.getColor(R.color.color_333333));
        binding.textMarket.setTextColor(ContextCompat.getColorStateList(MainActivity.this, R.color.color_333333));
        binding.textUser.setTextColor(ContextCompat.getColorStateList(MainActivity.this, R.color.color_333333));

        binding.textIndustry.setTypeface(Typeface.create(binding.textIndustry.getTypeface(), Typeface.NORMAL));
        binding.textMarket.setTypeface(Typeface.create(binding.textMarket.getTypeface(), Typeface.NORMAL));
        binding.textUser.setTypeface(Typeface.create(binding.textUser.getTypeface(), Typeface.NORMAL));

    }
}